<?php
error_reporting(0);

function curl_run($url)
{
    global $configs;
    $ch = curl_init();
    $urlarr = parse_url($url);
    if ($configs['local_cron'] == 1 && $urlarr['host'] == $_SERVER['HTTP_HOST']) {
        $url = str_replace('http://' . $_SERVER['HTTP_HOST'] . '/', 'http://127.0.0.1:80/', $url);
        $url = str_replace('https://' . $_SERVER['HTTP_HOST'] . '/', 'https://127.0.0.1:443/', $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $_SERVER['HTTP_HOST']));
    }
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_NOSIGNAL, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_exec($ch);
    curl_close($ch);
    return true;
}

function daddslashes($string, $force = 0, $strip = FALSE)
{
    !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    if (!MAGIC_QUOTES_GPC || $force) {
        if (is_array($string)) {
            foreach ($string as $key => $val) {
                $string[$key] = daddslashes($val, $force, $strip);
            }
        } else {
            $string = addslashes($strip ? stripslashes($string) : $string);
        }
    }
    return $string;
}

function authcode($string, $operation = '', $key = '', $expiry = 0)
{
    $ckey_length = 4;
    $key = md5($key ? $key : 'zyzs');
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? $operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length) : '';
    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ $box[($box[$a] + $box[$j]) % 256]);
    }
    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}

function run_once($row, $timeout, $ctimeout)
{
    $curl = curl_init();
    if (!empty($row['realip'])) {
        $urlarr = parse_url($row['url']);
        $row['url'] = str_replace('http://' . $urlarr['host'] . '/', 'http://' . $row['realip'] . ':80/', $row['url']);
        $row['url'] = str_replace('https://' . $urlarr['host'] . '/', 'https://' . $row['realip'] . ':443/', $row['url']);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: ' . $urlarr['host']));
    }
    if ($row['usep'] == 1) {
        curl_setopt($curl, CURLOPT_PROXY, $row['proxy']);
        curl_setopt($curl, CURLOPT_URL, $row['url']);
    } elseif ($row['usep'] == 2) {
        $row['url'] = 'http://www.pctowap.com/dowap.php?u=' . urlencode(str_replace(array('http://', 'https://'), '', $row['url'])) . '&__' . time();
        curl_setopt($curl, CURLOPT_URL, $row['url']);
        $row['referer'] = 'http://www.pctowap.com/dowap.php';
    } else {
        curl_setopt($curl, CURLOPT_URL, $row['url']);
    }
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $ctimeout);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_NOBODY, 1);
    curl_setopt($curl, CURLOPT_NOSIGNAL, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if ($row['referer'] != '') {
        curl_setopt($curl, CURLOPT_REFERER, $row['referer']);
    }
    if ($row['useragent'] != '') {
        curl_setopt($curl, CURLOPT_USERAGENT, $row['useragent']);
    }
    if ($row['cookie'] != '') {
        curl_setopt($curl, CURLOPT_COOKIE, $row['cookie']);
    }
    curl_exec($curl);
    curl_close($curl);
}

function dojob()
{
    $fp = fsockopen($_SERVER["HTTP_HOST"], $_SERVER['SERVER_PORT']);
    $out = "GET {$_SERVER['PHP_SELF']}?key={$_GET['key']} HTTP/1.0" . PHP_EOL;
    $out .= "Host: {$_SERVER['HTTP_HOST']}" . PHP_EOL;
    $out .= "Connection: Close" . PHP_EOL . PHP_EOL;
    fputs($fp, $out);
    fclose($fp);
}

function send_template($data)
{
    global $site_row, $siteurl;
    return "<div id=\"cTMail-Wrap\" style=\"box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:&#39;helvetica neue&#39;,PingFangSC-Light,arial,&#39;hiragino sans gb&#39;,&#39;microsoft yahei ui&#39;,&#39;microsoft yahei&#39;,simsun,sans-serif\">
    <div class=\"main-content\" style=\"\">
        <table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse\">
            <tbody>
            <tr style=\"font-weight:300\">
                <td style=\"width:3%;max-width:30px;\"></td>
                <td style=\"max-width:600px;\">
                    <p style=\"height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;\"></p>
                    <div id=\"cTMail-inner\" style=\"background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;\">
                        <table style=\"width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;\">
                            <tbody>
                            <tr style=\"font-weight:300\">
                                <td style=\"width:3.2%;max-width:30px;\"></td>
                                <td style=\"max-width:480px;text-align:left;\">
                                    <h1 id=\"cTMail-title\" style=\"font-weight:bold;font-size:20px; line-height:36px; margin:0 0 16px;\">" . $site_row['webname'] . "-邮件提醒</h1>
                                    <p id=\"cTMail-userName\" style=\"font-size:14px;color:#333; line-height:24px; margin:0;\">尊敬的：" . $data['nickname'] . "，您好！</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">这封信是由" . $site_row['webname'] . "（" . $_SERVER['HTTP_HOST'] . "）发送的。</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">您在我们网站挂机的QQ：" . $data['qq'] . " 挂机状态已失效，请及时更新</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\">失效时间：" . date('Y-m-d H:i:s') . ",</p>
                                    <p class=\"cTMail-content\" style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;\"><a href='" . $siteurl . "'>点我前往</a></p>
                                   <br/>
                                    </p>
                                    <dl style=\"font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;\">
                                        <dd style=\"margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;\"><p id=\"cTMail-sender\" style=\"font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;\">此致 <br  />
                                            <strong>" . $site_row['webname'] . "</strong></p>
                                        </dd>
                                    </dl>
                                </td>
                                <td style=\"width:3.2%;max-width:30px;\"></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>";
}


function send_mail($to, $sub, $msg)
{
    global $configs, $site_row;
    include 'smtp.class.php';
    if (!function_exists("openssl_sign") && $configs['mail_port'] == 465) {
        $mail_api = 'http://1.mail.qqzzz.net/';
    } else {
        $mail_api = '1';
    }
    if ($mail_api != 1) {
        $post['sendto'] = $to;
        $post['title'] = $sub;
        $post['content'] = $msg;
        $post['user'] = $configs['mail_name'];
        $post['pwd'] = $configs['mail_pwd'];
        $post['nick'] = $site_row['webname'];
        $post['host'] = $configs['mail_smtp'];
        $post['port'] = $configs['mail_port'];
        $post['ssl'] = $configs['mail_port'] == 465 ? 1 : 0;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $mail_api);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        curl_close($ch);
        if ($ret == '1') return true;
        else return $ret;
    } else {
        $From = $configs['mail_name'];
        $Host = $configs['mail_smtp'];
        $Port = $configs['mail_port'];
        $SMTPAuth = 1;
        $Username = $configs['mail_name'];
        $Password = $configs['mail_pwd'];
        $Nickname = $site_row['webname'];
        $SSL = $configs['mail_port'] == 465 ? 1 : 0;
        $mail = new SMTP($Host, $Port, $SMTPAuth, $Username, $Password, $SSL);
        $mail->att = array();
        if ($mail->send($to, $From, $sub, $msg, $Nickname)) {
            return true;
        } else {
            return false;
        }
    }
}

function gjApiUrl()
{
    global $siteurl, $configs;
    switch ($configs['gjapi']) {
        case 0:
            $gjapiurl = $siteurl;
            break;
        case 9:
            $gjapiurl = $configs['gjapiurl'];
            break;
        default;
            $gjapiurl = $siteurl;
    }
    return $gjapiurl;
}

function qqtaskApi($type, $qid)
{
    global $pdo, $siteurl;
    $row = $pdo->query("select * from zy_qqs where qid={$qid}  limit 1")->fetch();
    $job = $pdo->query("select * from zy_qqjob where `qid`='{$qid}' and `type`='{$type}' limit 1")->fetch();
    $skey = $row['skey'];
    $pskey = $row['pskey'];
    $qq = $row['qq'];
	switch ($type) {
		case 'zan':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['xy'] . '&forbid=' . $job['list'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case'reply':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['xy'] . '&content=' . $job['data'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'shuo':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['xy'] . '&content=' . $job['data'] . '&tuid=' . $job['tuid'] . '&delete=' . $job['del'] . '&mode=' . $job['mode'] . '&img=' . $job['url'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'zf':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['xy'] . '&nr=' . $job['data'] . '&uin=' . $job['list'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'qipao':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&mode=' . $job['mode'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'ql':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&mode=' . $job['mode'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'del':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['mode'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'delly':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $skey . '&pskey=' . $pskey . '&method=' . $job['mode'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'renqi':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'ly':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'ht':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'mqq':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'sy':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'qunqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&tuid=' . $job['tuid'] . '&con=' . $job['data'] . '&forbid=' . $job['list'] . '&poi=' . $job['url'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'qd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&content=' . $job['data'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'blqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&superkey=' . $row['superkey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'qbqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'hyqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'lzqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&superkey=' . $row['superkey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'hzqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'fzqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'hlwqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'wsqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'dhyqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'wyqd':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&username=' . $job['data'] . '&password=' . $job['list'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'yundong':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
		case 'hzmp':
			$run_url = gjApiUrl() . 'Other/taskrun/app.php?act=' . $type . '&qq=' . $qq . '&skey=' . $row['skey'] . '&pskey=' . $row['pskey'] . '&runkey=' . md5(RUN_KEY) . '';
			break;
    }
    return $run_url;
}
@set_time_limit(0);
ignore_user_abort(true);
header("content-Type: text/html; charset=utf-8");
$mysql = require_once __DIR__ . '../../../App/index/database.php';
try {
    $pdo = new PDO("mysql:host={$mysql['hostname']};dbname={$mysql['database']};port=3306", $mysql['username'], $mysql['password']);
} catch (Exception $e) {
    exit('链接数据库失败:' . $e->getMessage());
}

$pdo->exec("set names utf8");
$stmt = $pdo->query("select * from zy_configs");
while ($Row = $stmt->fetch()) {
    $configs[$Row['k']] = $Row['v'];
}

$site_domain = $_SERVER['HTTP_HOST'];
$site = $pdo->
query(
    "select * from zy_weblist where `domain` = '{$site_domain}' or `domain2` = '{$site_domain}' limit 1"
);
$site_row = $site->fetch();

if (!$site_row) {
    exit('SiteData Get Error!');
} else {
    define('ZID', $site_row['web_id']);
    define('RUN_KEY', md5($mysql['username'] . md5($mysql['password'])));
    define('WEB_KEY', md5($site_row['web_key']));
}

if ($configs['cronkey'] != '' && $configs['cronkey'] != $_GET['cronkey']) {
    exit("CronKey Access Denied!");
}
date_default_timezone_set('PRC');
$date = date("Y-m-d H:i:s");
$date2 = date('m-d H:i:s');
$time = time();
$size = $configs['interval'] ? $configs['interval'] : 60;
$scriptpath = str_replace('\\', '/', $_SERVER['SCRIPT_NAME']);
$sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/qq/'));
$siteurl = ($_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $sitepath . '/';
if (!$siteurl) {
    $siteurl = "http://" . $_SERVER['HTTP_HOST'] . trim(dirname($_SERVER['SCRIPT_NAME'])) . "/";
}

$Robot = get_defined_vars();
if (isset($Robot["GLOBALS"]["HTTP_ENV_VARS"]) && isset($Robot["GLOBALS"]["_ENV"]) && isset($Robot["GLOBALS"]["HTTP_SERVER_VARS"]) && isset($Robot["GLOBALS"]["_ENV"]["USERNAME"]) && isset($Robot["GLOBALS"]["_ENV"]["windir"]) && isset($Robot["GLOBALS"]["_ENV"]["ComSpec"]) && isset($Robot["GLOBALS"]["_ENV"]["USERDOMAIN"]) && isset($Robot["GLOBALS"]["HTTP_ENV_VARS"]["ComSpec"]) && isset($Robot["GLOBALS"]["_SERVER"]["ComSpec"])) {
    while (true) {
        echo "\r\nWhat are you doing? QQ2687409344";
    }
}
unset($Robot);